package cn.ichiva.luckysheet.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import static cn.ichiva.luckysheet.utils.test.rightGroup;

/**
 * TODO
 *
 * @author pzq
 * @version 1.0
 * @date 2021/12/27 13:52
 */
public class SheetDemo {
    public static void main(String[] args) {
        String datajson="{\n" +
                "\t\"data\": [{\n" +
                "\t\t\"amount\": \"100\",\n" +
                "\t\t\"month\": \"1\",\n" +
                "\t\t\"areaname\": \"华北\",\n" +
                "\t\t\"year\": \"2020\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"1\",\n" +
                "\t\t\"dept\": \"河北\",\n" +
                "\t\t\"settleamount\": \"100\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"202\",\n" +
                "\t\t\"month\": \"2\",\n" +
                "\t\t\"areaname\": \"华北\",\n" +
                "\t\t\"year\": \"2021\",\n" +
                "\t\t\"price\": \"6\",\n" +
                "\t\t\"id\": \"12\",\n" +
                "\t\t\"dept\": \"河南\",\n" +
                "\t\t\"settleamount\": \"202\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"205\",\n" +
                "\t\t\"month\": \"1\",\n" +
                "\t\t\"areaname\": \"华南\",\n" +
                "\t\t\"year\": \"2020\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"15\",\n" +
                "\t\t\"dept\": \"湖北\",\n" +
                "\t\t\"settleamount\": \"205\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"201\",\n" +
                "\t\t\"month\": \"2\",\n" +
                "\t\t\"areaname\": \"华南\",\n" +
                "\t\t\"year\": \"2021\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"20\",\n" +
                "\t\t\"dept\": \"长沙\",\n" +
                "\t\t\"settleamount\": \"201\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"200\",\n" +
                "\t\t\"month\": \"1\",\n" +
                "\t\t\"areaname\": \"华中\",\n" +
                "\t\t\"year\": \"2020\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"21\",\n" +
                "\t\t\"dept\": \"武汉\",\n" +
                "\t\t\"settleamount\": \"200\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"202\",\n" +
                "\t\t\"month\": \"2\",\n" +
                "\t\t\"areaname\": \"华中\",\n" +
                "\t\t\"year\": \"2021\",\n" +
                "\t\t\"price\": \"6\",\n" +
                "\t\t\"id\": \"22\",\n" +
                "\t\t\"dept\": \"南京\",\n" +
                "\t\t\"settleamount\": \"202\"\n" +
                "\t}]\n" +
                "}";
//        String str =  HttpRequest.sendGet("http://api.jeecg.com/mock/26/subtotal",null);
        JSONObject jsonObjectStr  = JSONObject.parseObject(datajson);
        List<Map<String,Object>> maps = (List<Map<String, Object>>) jsonObjectStr.get("data");
        String moban="[{\"name\":\"Cell\",\"index\":\"sheet_01\",\"order\":0,\"status\":1,\"celldata\":[{\"r\":0,\"c\":0,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"ht\":\"0\",\"m\":\"区域\",\"v\":\"区域\",\"mc\":{\"r\":0,\"c\":0,\"rs\":2,\"cs\":1}}},{\"r\":0,\"c\":1,\"v\":{\"ht\":\"0\",\"m\":\"省份\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"v\":\"省份\",\"mc\":{\"r\":0,\"c\":1,\"rs\":2,\"cs\":1}}},{\"r\":0,\"c\":2,\"v\":{\"v\":\"#groupHeadRight(year)\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"m\":\"#groupHeadRight(year)\",\"mc\":{\"cs\":3,\"rs\":1,\"r\":0,\"c\":2}}},{\"r\":0,\"c\":3,\"v\":{\"mc\":{\"r\":0,\"c\":2}}},{\"r\":0,\"c\":4,\"v\":{\"mc\":{\"r\":0,\"c\":2}}},{\"r\":0,\"c\":5,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":6,\"v\":{}},{\"r\":0,\"c\":7,\"v\":{}},{\"r\":0,\"c\":8,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":9,\"v\":{}},{\"r\":0,\"c\":10,\"v\":{}},{\"r\":0,\"c\":11,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":12,\"v\":{}},{\"r\":0,\"c\":13,\"v\":{}},{\"r\":0,\"c\":14,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":15,\"v\":{}},{\"r\":0,\"c\":16,\"v\":{}},{\"r\":0,\"c\":17,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":18,\"v\":{}},{\"r\":0,\"c\":19,\"v\":{}},{\"r\":0,\"c\":20,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":21,\"v\":{}},{\"r\":0,\"c\":22,\"v\":{}},{\"r\":0,\"c\":23,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":24,\"v\":{}},{\"r\":0,\"c\":25,\"v\":{}},{\"r\":0,\"c\":26,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":27,\"v\":{}},{\"r\":0,\"c\":28,\"v\":{}},{\"r\":0,\"c\":29,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":30,\"v\":{}},{\"r\":0,\"c\":31,\"v\":{}},{\"r\":0,\"c\":32,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":33,\"v\":{}},{\"r\":0,\"c\":34,\"v\":{}},{\"r\":0,\"c\":35,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":36,\"v\":{}},{\"r\":0,\"c\":37,\"v\":{}},{\"r\":0,\"c\":38,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":39,\"v\":{}},{\"r\":0,\"c\":40,\"v\":{}},{\"r\":0,\"c\":41,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":42,\"v\":{}},{\"r\":0,\"c\":43,\"v\":{}},{\"r\":0,\"c\":44,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":45,\"v\":{}},{\"r\":0,\"c\":46,\"v\":{}},{\"r\":0,\"c\":47,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":48,\"v\":{}},{\"r\":0,\"c\":49,\"v\":{}},{\"r\":0,\"c\":50,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":51,\"v\":{}},{\"r\":0,\"c\":52,\"v\":{}},{\"r\":0,\"c\":53,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":54,\"v\":{}},{\"r\":0,\"c\":55,\"v\":{}},{\"r\":0,\"c\":56,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":57,\"v\":{}},{\"r\":0,\"c\":58,\"v\":{}},{\"r\":0,\"c\":59,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":60,\"v\":{}},{\"r\":0,\"c\":61,\"v\":{}},{\"r\":0,\"c\":62,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":63,\"v\":{}},{\"r\":0,\"c\":64,\"v\":{}},{\"r\":0,\"c\":65,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":66,\"v\":{}},{\"r\":0,\"c\":67,\"v\":{}},{\"r\":0,\"c\":68,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":69,\"v\":{}},{\"r\":0,\"c\":70,\"v\":{}},{\"r\":0,\"c\":71,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}},{\"r\":0,\"c\":72,\"v\":{}},{\"r\":0,\"c\":73,\"v\":{}},{\"r\":1,\"c\":0,\"v\":{\"mc\":{\"r\":0,\"c\":0}}},{\"r\":1,\"c\":1,\"v\":{\"mc\":{\"r\":0,\"c\":1}}},{\"r\":1,\"c\":2,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"m\":\"单位\",\"v\":\"单位\"}},{\"r\":1,\"c\":3,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"m\":\"金额\",\"v\":\"金额\"}},{\"r\":1,\"c\":4,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"m\":\"小计1\",\"v\":\"小计1\"}},{\"r\":2,\"c\":0,\"v\":{\"m\":\"#groupt(areaname)\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"v\":\"#groupt(areaname)\"}},{\"r\":2,\"c\":1,\"v\":{\"m\":\"#group(dept)\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"v\":\"#group(dept)\"}},{\"r\":2,\"c\":2,\"v\":{\"m\":\"#select(price)\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"v\":\"#select(price)\"}},{\"r\":2,\"c\":3,\"v\":{\"m\":\"#select(amount)\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"v\":\"#select(amount)\"}},{\"r\":2,\"c\":4,\"v\":{\"m\":\"#select(price)\",\"ct\":{\"fa\":\"General\",\"t\":\"g\"},\"v\":\"#select(price)\"}},{\"r\":2,\"c\":5,\"v\":{\"ct\":{\"fa\":\"General\",\"t\":\"n\"}}}],\"jfgird_select_save\":[],\"luckysheet_select_save\":[{\"left\":572,\"width\":131,\"top\":258,\"height\":19,\"left_move\":572,\"width_move\":131,\"top_move\":258,\"height_move\":19,\"row\":[13,13],\"column\":[4,4],\"row_focus\":13,\"column_focus\":4}],\"config\":{\"merge\":{\"0_0\":{\"r\":0,\"c\":0,\"rs\":2,\"cs\":1},\"0_1\":{\"r\":0,\"c\":1,\"rs\":2,\"cs\":1},\"0_2\":{\"cs\":3,\"rs\":1,\"r\":0,\"c\":2}},\"borderInfo\":[{\"rangeType\":\"range\",\"borderType\":\"border-all\",\"style\":\"1\",\"color\":\"#000\",\"range\":[{\"row\":[0,1],\"column\":[0,3]}]},{\"rangeType\":\"range\",\"borderType\":\"border-all\",\"style\":\"1\",\"color\":\"#000\",\"range\":[{\"row\":[0,1],\"column\":[0,3]}]},{\"rangeType\":\"range\",\"borderType\":\"border-all\",\"style\":\"1\",\"color\":\"#000\",\"range\":[{\"row\":[0,1],\"column\":[0,3]}]},{\"rangeType\":\"range\",\"borderType\":\"border-none\",\"style\":\"1\",\"color\":\"#000\",\"range\":[{\"row\":[0,1],\"column\":[0,3]}]}],\"columnlen\":{\"0\":149,\"1\":126,\"2\":158,\"3\":135,\"4\":131,\"5\":141},\"customWidth\":{\"0\":1,\"1\":1,\"2\":1,\"3\":1,\"4\":1,\"5\":1},\"rowlen\":{\"0\":17},\"rowhidden\":{},\"customHeight\":{\"0\":1}},\"visibledatarow\":[18,38,58,78,98,118,138,158,178,198,218,238,258,278,298,318,338,358,378,398,418,438,458,478,498,518,538,558,578,598,618,638,658,678,698,718,738,758,778,798,818,838,858,878,898,918,938,958,978,998,1018,1038,1058,1078,1098,1118,1138,1158,1178,1198,1218,1238,1258,1278,1298,1318,1338,1358,1378,1398,1418,1438,1458,1478,1498,1518,1538,1558,1578,1598,1618,1638,1658,1678],\"visibledatacolumn\":[150,277,436,572,704,846,920,994,1068,1142,1216,1290,1364,1438,1512,1586,1660,1734,1808,1882,1956,2030,2104,2178,2252,2326,2400,2474,2548,2622,2696,2770,2844,2918,2992,3066,3140,3214,3288,3362,3436,3510,3584,3658,3732,3806,3880,3954,4028,4102,4176,4250,4324,4398,4472,4546,4620,4694,4768,4842,4916,4990,5064,5138,5212,5286,5360,5434,5508,5582,5656,5730,5804,5878],\"ch_width\":5998,\"rh_height\":1758,\"luckysheet_selection_range\":[],\"zoomRatio\":1,\"dynamicArray\":[],\"images\":{},\"scrollLeft\":0,\"scrollTop\":0,\"calcChain\":[],\"filter_select\":{},\"filter\":{},\"luckysheet_conditionformat_save\":[],\"luckysheet_alternateformat_save\":[],\"dataVerification\":{},\"hyperlink\":{}},{\"name\":\"Data\",\"index\":\"sheet_02\",\"order\":1,\"status\":0,\"celldata\":[]},{\"name\":\"Picture\",\"index\":\"sheet_03\",\"order\":2,\"status\":0,\"celldata\":[]}]";
        JSONArray jsonArray = (JSONArray) JSONObject.parse(moban);
        JSONObject jsonObject = (JSONObject) jsonArray.get(0);
        JSONArray jsonObjectList = jsonObject.getJSONArray("celldata");

        JSONObject jsonObjectconfig = jsonObject.getJSONObject("config");

        List groupList = new ArrayList();
        List selectList = new ArrayList();
        //解析模板 所有数据值
        for (int i = 0; i < jsonObjectList.size(); i++) {
            String v=jsonObjectList.getJSONObject(i).get("v").toString();
            JSONObject jsonObject1 =JSONObject.parseObject(v);
            if(!StringUtils.isEmpty(jsonObject1.get("m"))){
                if(jsonObject1.get("m").toString().lastIndexOf("select")!= -1){
                    String s = jsonObject1.get("m").toString().substring(jsonObject1.get("m").toString().indexOf("(")+1,jsonObject1.get("m").toString().indexOf(")"));
                    selectList.add(s);
                }
                //处理头部x轴坐标 发现头部右扩展关键字 扩展模板
                if(jsonObject1.get("m").toString().lastIndexOf("groupHeadRight")!= -1){
                    String s = jsonObject1.get("m").toString().substring(jsonObject1.get("m").toString().indexOf("(")+1,jsonObject1.get("m").toString().indexOf(")"));
                    groupList.add(s);

                    List<Map<String, Object>> list2 = maps.stream().collect(
                            Collectors.collectingAndThen(
                                    Collectors.toCollection(() ->new TreeSet<>(Comparator.comparing(m->m.get(s).toString()))),ArrayList::new
                            )
                    );    //根据value字段值去重，保留第一次放入list的map值
                    Map zhuti=rightGroup(jsonObjectList.getJSONObject(i),list2,s,jsonObjectList);
                    JSONObject jsonObject2 = jsonObjectList.getJSONObject(i);
                    JSONObject v2=(JSONObject)jsonObject2.get("v");
                    JSONObject meg=(JSONObject)v2.get("mc");
                    meg.get("r");
                    meg.get("c");
                    meg.get("rs");
                    meg.get("cs");
//                    移动合并的数据为 横行 r+rs-1  竖行 c+cs
//                    计算合并多少列
                    int cnum=  Integer.valueOf(meg.get("c").toString());
                    System.out.println("2");
                    zhuti.get("cell").toString();
                    String[] str = new String[]{"2","3","4"};
                    List<JSONObject> list=test.getlie(null,jsonObjectList,str);
                    for (int j = 0; j <list2.size() ; j++) {
                        for (int k = 0; k <list.size() ; k++) {
                            JSONObject a= list.get(k);
                            a.put("c","");
                        }

                    }

                    //获取整个列的数据 获取到关键字
                    System.out.println("123");
//                  扩展模板内容 以便后续处理

                }
            }
        }
        long end = System.currentTimeMillis();
        for (int i = 0; i < maps.size(); i++) {
            maps.get(i).get("dept");
        }
        //解析模板 所有配置值
        if(ObjectUtils.isNotEmpty(jsonObjectconfig)){
            jsonObjectconfig.get("merge");
        }
        System.out.println("123");
    }
}
